package design3;

/**
 * A calculator used to calculate the square root of numbers.
 */
public class SqrtCalculator {
	
	private Cache<Number, Number> numberToSqrt = new Cache<>();
	
	/**
	 * Gets the square root of the given number.
	 * 
	 * @param number
	 *    The number, for which to get the square root.
	 * @return
	 *    The square root of the given number.
	 */
	public Number getSqrt(Number number) {
		if (number == null) {
			throw new IllegalArgumentException("number should not be null.");
		}
		
		Number sqrt = numberToSqrt.get(number);
		if (sqrt == null) {
			sqrt = Math.sqrt(number.doubleValue());
			numberToSqrt.put(number, sqrt);
		}
		
		return sqrt;
	}

}